package News.dao;

import java.sql.*;
import java.util.ResourceBundle;

public class BaseDao {

    private static String driver; // 数据库驱动字符串
    private static String url; // 连接URL字符串
    private static String user; // 数据库用户名
    private static String password; // 用户密码
    Connection conn = null;// 数据连接对象

    /**
     * 静态初始化参数
     */
    static {init();}

    public static void init(){
        ResourceBundle rb=ResourceBundle.getBundle("conWSQ");
        driver = rb.getString("driver");
        url = rb.getString("url");
        user = rb.getString("user");
        password = rb.getString("password");
        try {
            Class.forName(driver);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

//    public static void main(String[] args) {
//        BaseDao b=new BaseDao();
//        b.getConn();
//    }
    /**
     * 获得连接
     * @return 返回的数据库连接
     */
    public Connection getConn(){
        try{
            conn= DriverManager.getConnection(url,user,password);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return conn;
    }

    /**
     * 关闭连接
     * @param conn 数据库连接
     * @param st Statement对象
     * @param rs 结果集
     */
    public void clossAll(Connection conn, Statement st, ResultSet rs){
        if(rs!=null){
            try{
                rs.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        if(st!=null){
            try{
                st.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        if(conn!=null){
            try{
                conn.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    }

}
